package org.nowcoder.huawei.c100;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 6：手机App 防沉迷系统
 */

public class HC006 {

    static class App {
        private String name;
        private int priority;
        private LocalTime start;
        private LocalTime end;

        public App(String name, int priority, String start, String end) {
            this.name = name;
            this.priority = priority;
            String[] vars = start.split(":");
            this.start = LocalTime.of(Integer.parseInt(vars[0]), Integer.parseInt(vars[1]));
            vars = end.split(":");
            this.end = LocalTime.of(Integer.parseInt(vars[0]), Integer.parseInt(vars[1]));
        }
    }

    public static void main(String[] args) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            int n = Integer.parseInt(br.readLine());
            int count = 0;
            List<App> apps = new ArrayList<>();
            while (count < n) {
                String[] vars = br.readLine().split(" ");
                String name = vars[0];
                int priority = Integer.parseInt(vars[1]);
                String start = vars[2];
                String end = vars[3];
                apps.add(new App(name, priority, start, end));
                count++;
            }
            String time = br.readLine();
            solve(apps, time);
        } catch (Exception ignore) {
        }
    }

    public static void solve(List<App> apps, String time) {
        App result = null;
        String[] vars = time.split(":");
        LocalTime now = LocalTime.of(Integer.parseInt(vars[0]), Integer.parseInt(vars[1]));
        for (App app : apps) {
            if (app.start.isBefore(now) && app.end.isAfter(now)) {
                if (result == null) {
                    result = app;
                } else {
                    if (app.priority > result.priority) {
                        result = app;
                    }
                }
            }
        }
        String name = result == null ? "NA" : result.name;
        System.out.println(name);
    }
}
